package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Device;
import com.sl.au.entity.ReplaceDevicePrice;
import com.sl.au.entity.ReplacementDevice;
import com.sl.au.entity.SinglePump;
import com.sl.au.entity.User;
import com.sl.au.repository.ReplaceDevicePriceRepository;
import com.sl.au.repository.ReplacementDeviceRepository;
import com.sl.au.service.ExcelService;
import com.sl.au.service.ReplacementDeviceService;


/**
 * 销售人员管理接口
 * @author XH
 *
 */
@Service("ReplacementDeviceService")
@Transactional
public class ReplacementDeviceServiceImpl extends BaseServiceImpl implements ReplacementDeviceService {

	@Autowired
	ReplacementDeviceRepository replacementDeviceRepository;
	
	@Autowired
	ReplaceDevicePriceRepository replaceDevicePriceRepository;
	
	@Autowired
	ExcelService excelService;

	@Override
	public ReplacementDeviceRepository getRepository() {
		// TODO Auto-generated method stub
		return this.replacementDeviceRepository;
	}

	@Override
	public List<ReplacementDevice> getPage(int start, int length, String key, String orderName, String dir) {
		Page<ReplacementDevice> pages=null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages=replacementDeviceRepository.getReplacementDevices(key, pageable);
		return pages.getContent();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public long importExcel(String fileName) {
		long count = 0;
		if (!StringUtils.hasText(fileName))
			return count;
		Map<String, Object> collections = new HashMap<>();
		excelService.importExcel("replaceDevice", collections, fileName);
		if (collections.size() < 1 || collections.get("items") == null)
			return count;
		List<ReplacementDevice> rows = (List<ReplacementDevice>) collections.get("items");
		List<ReplacementDevice> replacementDevices=this.getCheckedReDevice(rows);
        List<ReplaceDevicePrice> prices = new ArrayList<ReplaceDevicePrice>();
		if (null != replacementDevices && replacementDevices.size() > 0) {
			for(ReplacementDevice r:replacementDevices){
				String deviceName = r.getDeviceName();
				ReplaceDevicePrice p1 = new ReplaceDevicePrice();
				p1.setReplacementDevice(r);
				p1.setReDeviceName(deviceName);
				p1.setSuperior("成套处");
				prices.add(p1);
				ReplaceDevicePrice p2 = new ReplaceDevicePrice();
				p2.setReplacementDevice(r);
				p2.setReDeviceName(deviceName);
				p2.setSuperior("外贸处");
				prices.add(p2);
				ReplaceDevicePrice p3 = new ReplaceDevicePrice();
				p3.setReplacementDevice(r);
				p3.setReDeviceName(deviceName);
				p3.setSuperior("销售处");
				prices.add(p3);
				ReplaceDevicePrice p4 = new ReplaceDevicePrice();
				p4.setReplacementDevice(r);
				p4.setReDeviceName(deviceName);
				p4.setSuperior("售后服务处");
				prices.add(p4);
			}
            this.getRepository().save(replacementDevices);        
            replaceDevicePriceRepository.save(prices);
			count = replacementDevices.size();
		}
		return count;
	}

	@Override
	public List<ReplacementDevice> getCheckedReDevice(List<ReplacementDevice> rows) {
		List<ReplacementDevice> devices=new ArrayList<ReplacementDevice>();
		for(ReplacementDevice d:rows){
			String deviceName=d.getDeviceName();
			String type=d.getType();
			String material=d.getMaterial();
			long count=this.getRepository().getReCount(deviceName,type,material);
			if(count==0){
				devices.add(d);
			}
		}
		return devices;
	}

	@Override
	public void patch(ReplacementDevice replacementDevice) {
		ReplacementDevice oldReplace=this.getRepository().findOne(replacementDevice.getId());
		if(null==oldReplace) return;		
		BeanUtils.copyProperties(replacementDevice, oldReplace,"createTime","reDeviceName","reType","reMaterial");
		this.getRepository().save(oldReplace);
		
	}
}
